<template>
    <PageView>
        <a-card title="列表">
            <a-row :gutter="10" class="inputGroup">
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-range-picker v-model="time" :placeholder="['开始时间', '结束时间']" @change="getListTwo"/>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-input v-model="searchData.username" placeholder="账号" @keyup.enter="getListTwo"/>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-select allowClear v-model="searchData.type" style="width:100%" placeholder="类型"
                              @change="getListTwo">
                        <a-select-option :value="key" v-for="(value,key) in type_name">{{value}}</a-select-option>
                    </a-select>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-select allowClear v-model="searchData.sz_type" style="width:100%" placeholder="收支类型"
                              @change="getListTwo">
                        <a-select-option :value="key" v-for="(value,key) in sz_type_name">{{value}}</a-select-option>
                    </a-select>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-button-group>
                        <a-button type="primary" :loading="loading" @click="getListTwo">确定筛选</a-button>
                    </a-button-group>
                </a-col>
            </a-row>
            <a-table
                :loading="loading"
                :columns="columns"
                :dataSource="data"
                :pagination="{total,pageSize,showSizeChanger,current}"
                @change="changePagination"
                bordered :scroll="{x:true}">
            </a-table>
            <a-button type="primary" @click="exportExcel" style="float: right">导出表格</a-button>
        </a-card>
    </PageView>
</template>

<script type="text/jsx">
    import Finance from '../../api/Finance'
    export default {
        name: 'recordMoney',
        data () {
            return {
                loading: true,
                columns: [
                    {
                        title: '账号',
                        dataIndex: 'username',
                        customRender: (v) => <a>{v}</a>
                    },
                    {
                        title: '财务种类',
                        dataIndex: 'type_name',
                    },
                    {
                        title: '收支',
                        dataIndex: 'sz_type_name',
                        customRender: (v) => {
                            return <a-badge status={v==="收入"?'success':'error'} text={v}/>
                        }
                    },
                    {
                        title: '数额',
                        dataIndex: 'money',
                    },
                    {
                        title: '内容',
                        dataIndex: 'content',
                    },
                    {
                        title: '创建时间',
                        dataIndex: 'add_time',
                        customRender: (v) => this.timeOne(v)
                    },
                ],
                data: [],
                total: 0,
                pageSize: 10,
                current: 1,
                showSizeChanger: true,
                searchData: {
                    username: '',
                    sz_type: undefined,
                    type:undefined,
                },
                time: [],
                type_name:{},
                sz_type_name:{}
            }
        },
        activated () {
            this.getList()
        },
        methods: {
            getListTwo () {
                this.current = 1
                this.getList()
            },
            getList () {
                this.loading = true
                Finance.index({
                    page: this.current,
                    num: this.pageSize,
                    ...this.searchData,
                    begin: Date.parse(this.time[0]) / 1000 || undefined,
                    last: Date.parse(this.time[1]) / 1000 || undefined
                }).then(res => {
                    this.data = res.finances.map((value, key) => {
                        return { ...value, key }
                    })
                    this.type_name = res.type_name
                    this.sz_type_name = res.sz_type_name
                    this.loading = false
                    this.total = parseInt(res.count)
                })
            },
            changePagination (pagination) {
                Object.keys(pagination).forEach(val => {
                    this[val] = pagination[val]
                })
                this.getList()
            },
            //导出的方法
            exportExcel() {
                require.ensure([], () => {
                    const { export_json_to_excel } = require('../../excel/Export2Excel');
                    const tHeader = this.columns.map(v=>v.title);
                    const filterVal = this.columns.map(v=>v.dataIndex);
                    const list = JSON.parse(JSON.stringify(this.data)).map(v=>{
                        v.add_time = this.timeOne(v.add_time)
                        return v
                    });
                    const data = this.formatJson(filterVal, list);
                    export_json_to_excel(tHeader, data, '财务日志');
                })
            },

            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => v[j]))
            }
        }
    }
</script>

<style scoped lang="less">
    .inputGroup {
        > div {
            margin-bottom: 20px;
        }
    }
</style>